/**
 * 
 */
package com.vit.mep;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.PasswordEditField;

import com.vit.bb.VitScreen;
import com.vit.mep.manager.LoginManager;
import com.vit.mep.model.MepUser;

/**
 * @author crodriguez
 *
 */
public class LoginScreen extends VitScreen {

	protected MepApp _mepApp;	
	
	protected BasicEditField _txtUser;
	protected PasswordEditField _txtPassword;
	protected LabelField _lblStatus;
	
	protected ButtonField _btnLogin;
	protected ButtonField _btnCancel;
	
	
	public LoginScreen(MepApp mepApp){
		super();
		this._mepApp = mepApp;
		initComponents();
	}
	
	
	private void initComponents(){
		_txtUser =  new BasicEditField("Usuario: ", "", 40, BasicEditField.EDITABLE);
		_txtPassword = new PasswordEditField("Clave: ", "");
		_btnLogin  = new ButtonField("Login", ButtonField.CONSUME_CLICK);
		_btnCancel = new ButtonField("Cancel", ButtonField.CONSUME_CLICK);		
		
		_btnLogin.setChangeListener(new LoginBtnListener());
		_btnCancel.setChangeListener(new CancelBtnListener(this));
				
		
		_vfManager.add(_txtUser);
		_vfManager.add(_txtPassword);
		
		_hfManager.add(_btnLogin);
		_hfManager.add(_btnCancel);
		
		
		this.setTitle("MEP - Login");
		this.add(_hfBanner);
		this.add(_vfManager);
		this.add(_hfManager);
		
		
		_lblStatus = new LabelField();
		this.setStatus(_lblStatus);	
	}
	
	
	public MepUser getUser(){
		MepUser user = new MepUser();
		user.setPassword(_txtPassword.getText());
		user.setUsername(_txtUser.getText());
		
		return user;
	}
	
	
	private boolean validateUser(){
		String usr  = _txtUser.getText();		
		if(usr != null && !usr.equals("")){
			
			return true;
		}else{
			
			return false;
		}
		
	}

	
	/*-- ----------------------------------------------------- --*/
	
	class LoginBtnListener implements FieldChangeListener{
		
		public void fieldChanged(Field field, int context) {
			LoginManager manager = new LoginManager();
			
			MepUser muser = null; 
			
			try {
				muser = manager.login(getUser());	
			} catch (MepException e) {
				_lblStatus.setText(e.getAditionalMessage());
			}
						
			if(muser != null){
				_mepApp.setMepUser(muser);
				_mepApp.showReaderScreen();
			}else{
				_lblStatus.setText("Usuario o clave incorrectos");
			}
			
			
		}		
	}//LoginBtnListener
	
	
	
	class CancelBtnListener implements FieldChangeListener{
		
		LoginScreen loginScr;
		
		
		public CancelBtnListener(LoginScreen loginScr){
			this.loginScr = loginScr;
		}
		
		public void fieldChanged(Field field, int context) {
			_mepApp.endApp();
		}		
	}//CancelBtnListener
	
	
	/*-- ----------------------------------------------------- --*/
	
}//class






